﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace WHControlLib.Controls
{
    //**************************说明*************************************************************************************
    //  因为该控件是标题栏随Radius 的大小变化 并且当圆弧画图时 弧度的落点存在误差 造成点的填充和绘制误差，
    //  该控件目前采用这种形式，当折叠时存在变形的现象，可以通过减小 Border Width的值缩小误差变形和不显示IsShowBorder
    //  但不能根本解决 好处时 本控件能跟随控件形状变化 自动变化。
    //  要想精准控制需要采用 以像素点的形式绘制 圆角弧度 后期以新控件的形式实现，坏处，是提高后期有改变形状需求时候的UI界面开发难度
    //**************************说明结束************************************************************************************
    public class WHTitlePanel : Panel
    {

        public WHTitlePanel()
        {      //设置双缓冲
               //设置双缓冲开启禁止背景擦拭
            SetStyle(
                     ControlStyles.OptimizedDoubleBuffer
                     | ControlStyles.ResizeRedraw
                     | ControlStyles.Selectable
                     | ControlStyles.AllPaintingInWmPaint
                     | ControlStyles.UserPaint
                     | ControlStyles.SupportsTransparentBackColor,
                     true);

            ////背景定义为透明色   
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            BackColor = Color.Transparent;



        }

        //************全局参数定义***************
        public Rectangle MyRect = new Rectangle();
        public RectangleF DrawRct = new RectangleF();
        public Rectangle TitleRect = new Rectangle();
        public Rectangle CtrlBoxRect = new Rectangle();

        public bool OnMouse;
        public bool IsShowBody = true;

        public int MyoldHeight { get; set; }
        #region 属性定义
        //**************    通用属性 定义这里没有继承WHPanel重新写了一边方便已有Bug修改**************
        public enum FillColorDec
        {
            Vertical,
            Horizontal,
            LeftVH,
            RightVH

        }

        public enum TextAlign
        {
            CenterMiddle, CenterLeft, CenterRight, CenterButtom, CenterTop, TopLeft, TopRight, BottomLeft, BottomRight
        }


        private FillColorDec _myFillColorDec=FillColorDec.Vertical;
        [Category("A我的"), Description("当启用渐变色的渐变色变化方向，默认，垂直方向"), Browsable(true)]
        public FillColorDec MyFillColorDec
        {
            get { return _myFillColorDec; }
            set { _myFillColorDec = value; Invalidate(); }
        }



        private bool _isShowBorder = true;
        [Category("A我的"), Description("是否显示控件外边框，默认，true"), Browsable(true)]
        public bool IsShowBorder
        {
            get => _isShowBorder;
            set { _isShowBorder = value; Invalidate(); }

        }
        private Color _unEnableColor = Color.Gray;
        [Category("A我的"), Description("当不可用时候的颜色，默认，灰色"), Browsable(true)]
        public Color UnEnableColor
        {
            get => _unEnableColor;
            set { _unEnableColor = value; Invalidate(); }
        }
        private Color _borderColor = Color.Blue;
        /// <summary>
        /// 边框颜色
        /// </summary>
        [Category("A我的"), Description("边框颜色"), Browsable(true)]
        public Color BorderColor
        {
            get => _borderColor;
            set { _borderColor = value; Invalidate(); }
        }

        private float _borderWidth = 2;
        [Category("A我的"), Description("边框的宽度，默认 2"), Browsable(true)]
        public float BorderWidth
        {
            get => _borderWidth;
            set { _borderWidth = value; Invalidate(); }
        }
        private bool _isUseTwoColor;
        [Category("A我的"), Description("是否启用两种颜色填充，默认 false"), Browsable(true)]
        public bool IsUseTwoColor
        {
            get => _isUseTwoColor;
            set { _isUseTwoColor = value; Invalidate(); }
        }
        /// <summary>
        /// 第一种背景填充色
        /// </summary>
        private Color _firstFillcolor = Color.Beige;
        [Category("A我的"), Description(" 第一种背景填充色"), Browsable(true)]
        public Color FirstFillcolor
        {
            get => _firstFillcolor;
            set
            {
                _firstFillcolor = value;

                Invalidate();
            }
        }
        private Color _secondFillcolor = Color.Orange;
        [Category("A我的"), Description(" 第二种背景填充色"), Browsable(true)]
        public Color SecondFillcolor
        {
            get => _secondFillcolor;
            set
            {
                _secondFillcolor = value;

                Invalidate();
            }
        }

        //**********************
        public enum ShowRadiusDes
        {
            All, Top, Buttom, None
        }
        private ShowRadiusDes _myShowRadiusDes = ShowRadiusDes.All;
        [Category("A我的"), Description(" 哪里显示圆角，默认All都显示"), Browsable(true)]
        public ShowRadiusDes MyShowRadiusDes
        {
            get => _myShowRadiusDes;
            set { _myShowRadiusDes = value; Invalidate(); }
        }


        private float radius = 7.0f;

        /// <summary>
        /// 圆角大小比例
        /// </summary>
        [Category("A我的"), Description(" 圆角大小比例默认7.0,值越大圆角比例越小,标题栏高度跟随其变化，不能小于1.1"), Browsable(true)]
        public float Radius
        {
            get => radius;
            set
            {
                if (value > 1.1f)
                {
                    radius = value;
                    Invalidate();
                }
                else { radius = 1.1f; Invalidate(); }

            }
        }
        //展开 和折叠 的方法
        private bool UnSpreadBodyed { get; set; }
        private bool _isSpreadBody = true;
        [Category("A我的"), Description(" 是否展开标题栏下的工作区域，默认 true，是"), Browsable(true)]
        public bool IsSpreadBody
        {
            get => _isSpreadBody;
            set {
                if (value)
                {
                    if (UnSpreadBodyed)
                    {

                        Height = MyoldHeight;
                    }
                    else return;
               
            }
                else
                {
                    if (!UnSpreadBodyed)
                    {
                        MyoldHeight = Height;
                        Padding = new Padding(Left, Top, Right, MyRect.Height - TitleRect.Height);
                        if (IsShowBorder)
                        {
                            Height = (int)(TitleRect.Height + BorderWidth / 2 * 3);
                        }
                        else Height = TitleRect.Height;
                    }
                    else return;
                }

                UnSpreadBodyed = !UnSpreadBodyed;
                _isSpreadBody = value;
                Invalidate();
            }
        }

        private Color _titleTextColor = Color.Black;
        [Category("A我的标题栏"), Description("标题栏文本的颜色，默认，黑色 "), Browsable(true)]
        public Color TitleTextColor
        {
            get => _titleTextColor;
            set { _titleTextColor = value; Invalidate(); }
        }

        private Font _titleTextFont = new Font("宋体", 12);
        [Category("A我的标题栏"), Description("标题栏显示文字字体，默认，当前窗体默认字体 "), Browsable(true)]
        public Font TitleTextFont
        {
            get => _titleTextFont;
            set { _titleTextFont = value; Invalidate(); }
        }
        private FillColorDec _titleFillColorDec=FillColorDec.Vertical;
        [Category("A我的标题栏"), Description("标题栏渐变色的方向，默认，垂直 "), Browsable(true)]
        public FillColorDec TitleFillColorDec
        {
            get { return _titleFillColorDec; }
            set { _titleFillColorDec = value; Invalidate(); }
        }

        private Color _titleSecondColor=Color.YellowGreen;
        [Category("A我的标题栏"), Description("标题栏第二中颜色，默认，黄绿色 "), Browsable(true)]
        public Color TitleSecondColor
        {
            get { return _titleSecondColor; }
            set { _titleSecondColor = value; Invalidate(); }
        }

        private bool _isTitleUseTwoColor=false;
        [Category("A我的标题栏"), Description("标题栏是否启用渐变色，默认，false "), Browsable(true)]
        public bool IsTitleUseTwoColor
        {
            get { return _isTitleUseTwoColor; }
            set { _isTitleUseTwoColor = value; Invalidate(); }
        }

        public enum TitleTextAglin { Center, Left }
        private TitleTextAglin _myTitleTextAglin = TitleTextAglin.Center;
        [Category("A我的标题栏"), Description("标题栏 文字的对齐方向，默认，中间 "), Browsable(true)]
        public TitleTextAglin MyTitleTextAglin
        {
            get => _myTitleTextAglin;
            set { _myTitleTextAglin = value; Invalidate(); }
        }

        private string _titleText = "信息面板";
        [Category("A我的标题栏"), Description("标题栏 的 文字，默认，信息 "), Browsable(true)]
        public string TitleText
        {
            get => _titleText;
            set { _titleText = value; Invalidate(); }
        }
        private Color _titleBackColor = Color.GreenYellow;
        [Category("A我的标题栏"), Description("标题栏背景色，默认，绿色 "), Browsable(true)]
        public Color TitleBackColor
        {
            get => _titleBackColor;
            set { _titleBackColor = value; Invalidate(); }
        }


        private bool _isShowCtrlBox = true;
        [Category("A我的标题栏"), Description("是否显示标题栏上的控制按钮，默认，true 显示 "), Browsable(true)]
        public bool IsShowCtrlBox
        {
            get => _isShowCtrlBox;
            set { _isShowCtrlBox = value; Invalidate(); }
        }
        private Color _ctrlShapeOnMouseColor = Color.DarkRed;
        [Category("A我的标题栏"), Description("标题栏上的控制按钮鼠标一上去的颜色，默认 白色 "), Browsable(true)]
        public Color CtrlShapeOnMouseColor
        {
            get => _ctrlShapeOnMouseColor;
            set { _ctrlShapeOnMouseColor = value; Invalidate(); }
        }

        private Color _ctrlShapeColor = Color.Orange;
        [Category("A我的标题栏"), Description("标题栏上的控制按钮图形的颜色，默认，橙色"), Browsable(true)]
        public Color CtrlShapeColor
        {
            get => _ctrlShapeColor;
            set { _ctrlShapeColor = value; Invalidate(); }
        }


        private float _ctrlShapeBorderWidth = 3.0f;
        [Category("A我的标题栏"), Description("标题栏上的控制按钮边框的宽度，默认，3.0f "), Browsable(true)]
        public float CtrlShapeBorderWidth
        {
            get => _ctrlShapeBorderWidth;
            set { _ctrlShapeBorderWidth = value; Invalidate(); }
        }
        private float _ctrlBoxBl = 0.6f;
        [Category("A我的标题栏"), Description("标题栏上的控制按钮相对标题栏的大小，默认，0.6f倍不能大于1 "), Browsable(true)]
        public float CtrlBoxBl
        {
            get => _ctrlBoxBl;
            set
            {
                if (value > 1 || value <= 0.1)
                {
                    _ctrlBoxBl = 1;
                }
                else

                    _ctrlBoxBl = value; Invalidate();
            }
        }

        private float _ctrlBoxRightXBl = 0.1f;
        [Category("A我的标题栏"), Description("标题栏上的控制按钮相对右边框的距离比例，默认，0.1f倍 ,不能大于1 小于1 "), Browsable(true)]
        public float CtrlBoxRightXBl
        {
            get => _ctrlBoxRightXBl;
            set
            {
                if (value > 1 || value <= 0.01)
                {
                    value = 0.2f;
                }
                else
                    _ctrlBoxRightXBl = value; Invalidate();
            }
        }

        private Size _ctrlBoxOffset = new Size(10, 10);
        [Category("A我的标题栏"), Description("标题栏上的控制按钮相对标题栏的位置的位移量，默认，10,10 "), Browsable(true)]
        public Size CtrlBoxOffset
        {
            get => _ctrlBoxOffset;
            set { _ctrlBoxOffset = value; Invalidate(); }
        }


        #endregion

        public void BeginPaintIni()
        {
            MyRect = ClientRectangle;
            ////确定标题栏的大小
            if (IsShowBorder)
            {
                if (IsSpreadBody)
                {
                    TitleRect.X = (int)(MyRect.X + BorderWidth / 2);
                    TitleRect.Y = (int)(MyRect.Y + BorderWidth / 2);
                    TitleRect.Height = (int)(MyRect.Height / Radius - BorderWidth);
                    TitleRect.Width = (int)(MyRect.Width - BorderWidth);
                }
                else
                {
                    TitleRect = MyRect;
                }
            }

            else
            {
                if (IsSpreadBody)
                {
                    TitleRect.X = MyRect.X;
                    TitleRect.Y = MyRect.Y;
                    TitleRect.Height = (int)(MyRect.Height / Radius);
                    TitleRect.Width = MyRect.Width;
                }
                else
                {
                    TitleRect = MyRect;

                }
            }



        }

        protected override void OnPaint(PaintEventArgs e)
        {
            //base.OnPaint(e);
            BeginPaintIni();
            Graphics Myg = e.Graphics;
            Myg.SmoothingMode = SmoothingMode.AntiAlias;
            Myg.CompositingQuality = CompositingQuality.HighQuality;
            Myg.InterpolationMode = InterpolationMode.HighQualityBicubic;
           if (IsShowBorder)
          {
          //如果是展开状态就先填充整体
            if (IsSpreadBody)
            {
                FillBody(Myg);
               FillTitle(Myg,TitleRect);
                DrawBody(Myg);
                   
            }
            else
            {
                FillTitle(Myg,TitleRect);
                  //画标题栏边框
                DrawTitleBorder(Myg,TitleRect,BorderWidth,Radius);
            }
              
                }
            else
            {
                //如果是展开状态就先填充整体
                if (IsSpreadBody)
                {
                    FillBody(Myg);
                    FillTitle(Myg,TitleRect);
              

                }
                else
                {
                    FillTitle(Myg,TitleRect);
             
                }


            }
            DrawTitleText(Myg, TitleRect);
        }

        public void FillBody(Graphics Myg)
        {
            //不使用渐变色的情况
            GraphicsPath mybodypath = GetMyBodyPath(MyRect, BorderWidth, Radius);

            //using (SolidBrush BodyfillBrush = new SolidBrush(FirstFillcolor))
            //{
            //    Myg.FillPath(BodyfillBrush, mybodypath);
            //}

            //当是可用状态时候
            if (Enabled)
            {
                //当启用渐变色显示
                if (IsUseTwoColor)
                {
                    LinearGradientBrush FillBrush;

                    switch (MyFillColorDec)
                    {
                        case FillColorDec.Vertical:

                            FillBrush = new LinearGradientBrush(MyRect, FirstFillcolor, SecondFillcolor, LinearGradientMode.Vertical);
                            break;
                        case FillColorDec.Horizontal:

                            FillBrush = new LinearGradientBrush(MyRect, FirstFillcolor, SecondFillcolor, LinearGradientMode.Horizontal);
                            break;
                        case FillColorDec.LeftVH:

                            FillBrush = new LinearGradientBrush(MyRect, FirstFillcolor, SecondFillcolor, LinearGradientMode.ForwardDiagonal);
                            break;

                        case FillColorDec.RightVH:

                            FillBrush = new LinearGradientBrush(MyRect, FirstFillcolor, SecondFillcolor, LinearGradientMode.BackwardDiagonal);
                            break;
                        default:

                            FillBrush = new LinearGradientBrush(MyRect, FirstFillcolor, SecondFillcolor, LinearGradientMode.Vertical);
                            break;
                    }

                    Myg.FillPath(FillBrush, mybodypath);
                    //释放资源
                    FillBrush.Dispose();

                }
                //不起用渐变色显示
                else
                {
                    using (SolidBrush backBrush = new SolidBrush(FirstFillcolor))
                    {
                        Myg.FillPath(backBrush, mybodypath);
                    }
                }
            }
            //当不可用状态时候
            else
            {
                using (SolidBrush backBrush = new SolidBrush(UnEnableColor))
                {
                    Myg.FillPath(backBrush, mybodypath);
                }
            }






        }
        private void DrawBody(Graphics Myg)

        {
            GraphicsPath mybodypath = GetMyBodyPath(MyRect, BorderWidth, Radius);

                using (Pen mypen = new Pen(new SolidBrush(BorderColor), BorderWidth))
                {
                    Myg.DrawPath(mypen, mybodypath);
                }
     }
    
       public void FillTitle(Graphics Myg,Rectangle TitleRect)
        {
            GraphicsPath TitleFillPath = GetTitleFillPath(TitleRect, BorderWidth, Radius); 

            if (Enabled)
            {

    
                if (IsTitleUseTwoColor)
                {
                    LinearGradientBrush TitleFillBrush;
                    switch (TitleFillColorDec)
                    {
                        case FillColorDec.Vertical:
                            TitleFillBrush = new LinearGradientBrush(TitleRect, TitleBackColor, TitleSecondColor, LinearGradientMode.Vertical);
                            break;
                        case FillColorDec.Horizontal:
                            TitleFillBrush = new LinearGradientBrush(TitleRect, TitleBackColor, TitleSecondColor, LinearGradientMode.Horizontal);
                            break;
                        case FillColorDec.LeftVH:
                            TitleFillBrush = new LinearGradientBrush(TitleRect, TitleBackColor, TitleSecondColor, LinearGradientMode.ForwardDiagonal);
                            break;
                        case FillColorDec.RightVH:
                            TitleFillBrush = new LinearGradientBrush(TitleRect, TitleBackColor, TitleSecondColor, LinearGradientMode.BackwardDiagonal);
                            break;
                        default:
                            TitleFillBrush = new LinearGradientBrush(TitleRect, TitleBackColor, TitleSecondColor, LinearGradientMode.Vertical);
                            break;
                    }

                    Myg.FillPath(TitleFillBrush, TitleFillPath);
                    //释放资源
                    TitleFillBrush.Dispose();

                }
                //不起用渐变色显示
                     else
                     {
                         using (SolidBrush backBrush = new SolidBrush(TitleBackColor))
                            {
                        Myg.FillPath(backBrush, TitleFillPath);
                         }
                       }
            }  
            //当不可用状态时候
            else
            {
                using (SolidBrush backBrush = new SolidBrush(UnEnableColor))
                {
                    Myg.FillPath(backBrush, TitleFillPath);
                }
            }
                   
               if (IsShowCtrlBox)
            {
                DrawCtrlBox(Myg, TitleRect);
            }
       
      
        }
        private void DrawTitleBorder(Graphics Myg, Rectangle titleRect, float BorderWidth, float Radius)
        {
            GraphicsPath TitlePath = new GraphicsPath();
            float PenWidth = BorderWidth / 2;
            float HWheight;
            HWheight = titleRect.Height;
            Rectangle myrect = new Rectangle();
            myrect = titleRect;
            //定义四角小正方形的长度  这里不减一半线宽会发现画外边框两边的对不齐
            float w = HWheight - BorderWidth;
            float Pw = HWheight / 2;
            //当显示圆角时候的形状定义

            RectangleF topLeftRect = new RectangleF(myrect.X + PenWidth, myrect.Y + PenWidth, HWheight, HWheight);
            RectangleF topRightRect = new RectangleF(myrect.X + myrect.Width - HWheight - PenWidth, topLeftRect.Y, HWheight, HWheight);

            //12个点

            PointF leftTopLeft = new PointF(myrect.X + PenWidth, myrect.Y + PenWidth + Pw-1);
            PointF LeftTopRight = new PointF(myrect.X + PenWidth + Pw-1, myrect.Y + PenWidth);
            PointF rightTopRight = new PointF(myrect.X + myrect.Width - PenWidth, myrect.Y + PenWidth + Pw-1);
            PointF rightTopLeft = new PointF(myrect.X + myrect.Width - PenWidth - Pw+1, myrect.Y + PenWidth);
            ////标题底部 横线的两个端点
            PointF titleLinePointLeft = new PointF(titleRect.X +1, titleRect.Y + titleRect.Height-PenWidth);
            PointF titleLinePointRight = new PointF(titleRect.X + titleRect.Width-1 , titleLinePointLeft.Y);
            using (Pen titlePen = new Pen(BorderColor, BorderWidth))
            {
                switch (MyShowRadiusDes)
                {
                    case ShowRadiusDes.All:

                        ////画标题栏形状的路径
                        ///
                        Myg.DrawArc(titlePen, topLeftRect, 180, 90);
                        Myg.DrawArc(titlePen, topRightRect, 270, 90);

                        Myg.DrawLine(titlePen, leftTopLeft, titleLinePointLeft);
                        Myg.DrawLine(titlePen, rightTopRight, titleLinePointRight);
                        Myg.DrawLine(titlePen, LeftTopRight, rightTopLeft);
                        Myg.DrawLine(titlePen, titleLinePointLeft, titleLinePointRight);
                        break;
                    case ShowRadiusDes.Top:

                        ////标题栏形状的路径
                        ///
                        Myg.DrawArc(titlePen, topLeftRect, 180, 90);
                        Myg.DrawArc(titlePen, topRightRect, 270, 90);

                        Myg.DrawLine(titlePen, leftTopLeft, titleLinePointLeft);
                        Myg.DrawLine(titlePen, rightTopRight, titleLinePointRight);
                        Myg.DrawLine(titlePen, LeftTopRight, rightTopLeft);
                        Myg.DrawLine(titlePen, titleLinePointLeft, titleLinePointRight);

                        break;
                    case ShowRadiusDes.Buttom:

                        ////标题栏形状的路径
                        ///
                 
                        Myg.DrawRectangle(titlePen, myrect);


                        break;
                    case ShowRadiusDes.None:

                        ////标题栏形状的路径
                        ///
             
                        
                        Myg.DrawRectangle(titlePen, titleRect);
                        break;
                    default:
                        //myrect.X = (int)(myrect.X + PenWidth);
                        //myrect.Y = (int)(myrect.Y + PenWidth);
                        //myrect.Width = myrect.Width - (int)BorderWidth;
                        //myrect.Height = myrect.Height - (int)BorderWidth;
                        ////标题栏形状的路径
                        Myg.DrawRectangle(titlePen, titleRect);
                        break;
                }
            }
        }
        public GraphicsPath GetTitleFillPath(Rectangle titleRect, float BorderWidth, float Radius)
        {
            GraphicsPath TitlePath = new GraphicsPath();
            float PenWidth = BorderWidth / 2;
            float HWheight;
            HWheight = titleRect.Height;
            Rectangle myrect = new Rectangle();
            myrect = titleRect;
            //定义四角小正方形的长度  这里不减一半线宽会发现画外边框两边的对不齐
            float w = HWheight - BorderWidth;
            float Pw = HWheight / 2;
            //当显示圆角时候的形状定义
            RectangleF topLeftRect = new RectangleF(myrect.X + PenWidth, myrect.Y + PenWidth, HWheight, HWheight);
            RectangleF topRightRect = new RectangleF(myrect.X + myrect.Width - HWheight - PenWidth, topLeftRect.Y, HWheight, HWheight);
            PointF leftTopLeft = new PointF(myrect.X + PenWidth, myrect.Y + PenWidth + Pw);
            PointF rightTopRight = new PointF(myrect.X + myrect.Width - PenWidth, myrect.Y + PenWidth + Pw );
            TitlePath.ClearMarkers();

            //确定标题栏大小

            ////标题底部 横线的两个端点
            //PointF titleLinePointLeft = new PointF(titleRect.X + PenWidth, titleRect.Y + titleRect.Height);
            //PointF titleLinePointRight = new PointF(titleRect.X + titleRect.Width - PenWidth, titleRect.Y + titleRect.Height);
            PointF titleLinePointLeft;
            PointF titleLinePointRight;

         titleLinePointLeft = new PointF(titleRect.X + PenWidth, titleRect.Y + titleRect.Height-PenWidth);
          titleLinePointRight = new PointF(titleRect.X + titleRect.Width - PenWidth, titleRect.Y + titleRect.Height - PenWidth);
            switch (MyShowRadiusDes)
            {
                case ShowRadiusDes.All:

                    ////标题栏形状的路径
                    TitlePath.AddArc(topLeftRect, 180, 90);
                    TitlePath.AddArc(topRightRect, 270, 90);
                    TitlePath.AddLine(titleLinePointLeft, titleLinePointRight);
                    TitlePath.AddLine(leftTopLeft, titleLinePointLeft);
                    TitlePath.AddLine(rightTopRight, titleLinePointRight);
                    TitlePath.CloseFigure();

        
                    TitlePath.CloseFigure();

                    break;
                case ShowRadiusDes.Top:

                    ////标题栏形状的路径
                    TitlePath.AddArc(topLeftRect, 180, 90);
                    TitlePath.AddArc(topRightRect, 270, 90);
                    TitlePath.AddLine(titleLinePointLeft, titleLinePointRight);
                    TitlePath.AddLine(leftTopLeft, titleLinePointLeft);
                    TitlePath.AddLine(rightTopRight, titleLinePointRight);
                    TitlePath.CloseFigure();
                    break;
                case ShowRadiusDes.Buttom:

                    ////标题栏形状的路径
                    myrect.X = (int)(myrect.X );
                    myrect.Y = (int)(myrect.Y );
                    myrect.Width = myrect.Width - (int)PenWidth;
                    myrect.Height = myrect.Height - (int)PenWidth;
                    TitlePath.AddRectangle(myrect);
                    break;
                case ShowRadiusDes.None:
                    myrect.X = (int)(myrect.X+PenWidth);
                    myrect.Y = (int)(myrect.Y);
                    myrect.Width = myrect.Width+ -(int)BorderWidth ;
                    myrect.Height = myrect.Height - (int)PenWidth;
                    ////标题栏形状的路径
                    TitlePath.AddRectangle(myrect);

                    break;
                default:
                    myrect.X = (int)(myrect.X + PenWidth);
                    myrect.Y = (int)(myrect.Y);
                    myrect.Width = myrect.Width + -(int)BorderWidth;
                    myrect.Height = myrect.Height - (int)PenWidth;
                    ////标题栏形状的路径
                    TitlePath.AddRectangle(myrect);
                    break;
            }

            return TitlePath;


        }

        public GraphicsPath GetMyBodyPath(Rectangle MYRect, float BorderWidth, float Radius)
        {
            GraphicsPath path = new GraphicsPath();
            float PenWidth = BorderWidth / 2;
            float HWheight = MYRect.Height / Radius;
            //定义四角小正方形的长度  这里不减一半线宽会发现画外边框两边的对不齐
            float w = HWheight - BorderWidth;
            float Pw = HWheight / 2;
            //当不显示圆角时候的矩形
            Rectangle NonedrawRect = new Rectangle();
            NonedrawRect.X = (int)(MyRect.X + PenWidth);
            NonedrawRect.Y = (int)(MyRect.Y + PenWidth);
            NonedrawRect.Width = (int)(MyRect.Width - BorderWidth);
            NonedrawRect.Height = (int)(MyRect.Height - BorderWidth);
            //当显示圆角时候的形状定义
            RectangleF topLeftRect = new RectangleF(MYRect.X + PenWidth, MYRect.Y + PenWidth, HWheight, HWheight);
            RectangleF topRightRect = new RectangleF(MYRect.X + MYRect.Width - HWheight - PenWidth, topLeftRect.Y, HWheight, HWheight);
            RectangleF bottomLeftRect = new RectangleF(topLeftRect.X, MYRect.Y + MYRect.Height - HWheight - PenWidth, HWheight, HWheight);
            RectangleF bottomRightRect = new RectangleF(MyRect.X+Width-HWheight-PenWidth, bottomLeftRect.Y, HWheight , HWheight);

            //12个点
            PointF leftTop = new PointF(MYRect.X + PenWidth, MYRect.Y + PenWidth);
            PointF leftTopLeft = new PointF(MYRect.X + PenWidth, MYRect.Y + PenWidth + Pw);
            PointF LeftTopRight = new PointF(MYRect.X + PenWidth + Pw, MYRect.Y + PenWidth);
            PointF Leftbuttom = new PointF(MYRect.X + PenWidth, MYRect.Y + MYRect.Height - PenWidth);
            PointF rightTop = new PointF(MYRect.X + MYRect.Width - PenWidth, MYRect.Y + PenWidth);
            PointF rightTopRight = new PointF(MYRect.X + MYRect.Width - PenWidth, MYRect.Y + PenWidth + Pw);
            PointF rightTopLeft = new PointF(MYRect.X + MYRect.Width - PenWidth - Pw, MYRect.Y + PenWidth);
            PointF rightbuttom = new PointF(MYRect.X + MYRect.Width - PenWidth, MYRect.Y + MYRect.Height - PenWidth);
            PointF LeftbuttomLeft = new PointF(MYRect.X + MYRect.Width - PenWidth - Pw, MYRect.Y + MYRect.Height - PenWidth);
            PointF LeftbuttomRight = new PointF(MYRect.X + PenWidth + Pw, MYRect.Y + MYRect.Height - PenWidth);
            PointF rightbuttomRight = new PointF(MYRect.X + MYRect.Width - PenWidth, MYRect.Y + MYRect.Height - PenWidth - Pw);
            PointF rightbuttomLeft = new PointF(MYRect.X + MYRect.Width - PenWidth - Pw, MYRect.Y + MYRect.Height - PenWidth);
            path.ClearMarkers();
            switch (MyShowRadiusDes)
            {
                case ShowRadiusDes.All:
                    path.AddArc(topLeftRect, 180, 90);
                    path.AddArc(topRightRect, 270, 90);
                    path.AddArc(bottomRightRect, 0, 90);
                    path.AddArc(bottomLeftRect, 90, 90);
                    path.CloseAllFigures();

                    break;
                case ShowRadiusDes.Top:
                    path.AddArc(topLeftRect, 180, 90);
                    path.AddArc(topRightRect, 270, 90);

                    path.AddLine(rightbuttom, Leftbuttom);
                    path.AddLine(Leftbuttom, leftTopLeft);
                    path.CloseAllFigures();

                    break;
                case ShowRadiusDes.Buttom:
                    path.AddArc(bottomRightRect, 0, 90);
                    path.AddArc(bottomLeftRect, 90, 90);
                    path.AddLine(leftTop, rightTop);
                    path.AddLine(rightTopRight, rightbuttomRight);
                    path.CloseAllFigures();


                    break;
                case ShowRadiusDes.None:
                    path.AddRectangle(NonedrawRect);

                    break;
                default:
                    path.AddRectangle(NonedrawRect);

                    break;
            }

            return path;



        }




        public void DrawCtrlBox(Graphics Myg, Rectangle TitleRect)
        {
            //const float ShapeBL = 0.7f;

            float ctrlH = TitleRect.Height * CtrlBoxBl;
            float ctrlW = ctrlH;
            CtrlBoxRect.X = (int)(TitleRect.X + TitleRect.Width * (1 - CtrlBoxRightXBl) - ctrlW + CtrlBoxOffset.Width);
            CtrlBoxRect.Y = (int)(TitleRect.Y + TitleRect.Height / 2 - ctrlH / 2);
            CtrlBoxRect.Height = CtrlBoxRect.Width = (int)ctrlH;


            ////////画箭头的形状***********可以修改更好的效果，只有重新定义箭头的顶点***目前不做更改*****
            ////此状态下没有减少这个比例会造成配色偏差，因为箭头的 线宽 引起图像不能完全合并造成
            float delShapeborderwidth = CtrlShapeBorderWidth / 2;
            ////定义箭头的顶点
            PointF PshapeTopLeft = new PointF(CtrlBoxRect.X + delShapeborderwidth, CtrlBoxRect.Y + ctrlH / 2);
            PointF PshapeTopRight = new PointF(CtrlBoxRect.X + CtrlBoxRect.Width - delShapeborderwidth, CtrlBoxRect.Y + (ctrlH / 2));
            PointF PshapeTop1 = new PointF((CtrlBoxRect.X + ctrlH / 2) + CtrlShapeBorderWidth / 4, CtrlBoxRect.Y + delShapeborderwidth);
            PointF PshapeTop2 = new PointF((CtrlBoxRect.X + ctrlH / 2) - CtrlShapeBorderWidth / 4, CtrlBoxRect.Y + delShapeborderwidth);
            PointF PshapeButtom1 = new PointF((CtrlBoxRect.X + ctrlH / 2) + CtrlShapeBorderWidth / 4, (CtrlBoxRect.Y + ctrlH) - delShapeborderwidth);
            PointF PshapeButtom2 = new PointF((CtrlBoxRect.X + ctrlH / 2) - CtrlShapeBorderWidth / 4, (CtrlBoxRect.Y + ctrlH) - delShapeborderwidth);
            ////*********************定义箭头的顶点结束*****************
            if (IsShowCtrlBox)
            {
                if (OnMouse)
                {
                    using (Pen onmouseShapePen = new Pen(CtrlShapeOnMouseColor, CtrlShapeBorderWidth))
                    {  //如果是展开状态箭头朝上
                        if (IsSpreadBody)
                        {
                            Myg.DrawLine(onmouseShapePen, PshapeTopLeft, PshapeButtom1);
                            Myg.DrawLine(onmouseShapePen, PshapeButtom2, PshapeTopRight);

                        }
                        else
                        {
                            Myg.DrawLine(onmouseShapePen, PshapeTopLeft, PshapeTop1);
                            Myg.DrawLine(onmouseShapePen, PshapeTop2, PshapeTopRight);
                        }
                    }

                }
                else
                {
                    using (Pen shapePen = new Pen(CtrlShapeColor, CtrlShapeBorderWidth))
                    {

                        //如果是展开状态箭头朝上
                        if (IsSpreadBody)
                        {
                            Myg.DrawLine(shapePen, PshapeTopLeft, PshapeButtom1);
                            Myg.DrawLine(shapePen, PshapeButtom2, PshapeTopRight);

                        }
                        else
                        {
                            Myg.DrawLine(shapePen, PshapeTopLeft, PshapeTop1);
                            Myg.DrawLine(shapePen, PshapeTop2, PshapeTopRight);
                        }
                    }
                }

            }

            }
       


        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            if (IsShowCtrlBox && RectangleToScreen(CtrlBoxRect).Contains(MousePosition))
            {
                OnMouse = true;
                Invalidate(CtrlBoxRect);
            }
            else
            {
                OnMouse = false;
                Invalidate(CtrlBoxRect);
            }

        }

        protected override void OnMouseLeave(EventArgs e)
        {
            if (IsShowCtrlBox)
            {
                OnMouse = false;
                Invalidate(CtrlBoxRect);
            }

            base.OnMouseLeave(e);


        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);
            if (IsShowCtrlBox && RectangleToScreen(CtrlBoxRect).Contains(MousePosition))
            {
                if (IsSpreadBody)
                {
                    IsSpreadBody = false;
                    //MyoldHeight = Height;
                    //Padding = new Padding(Left, Top, Right, MyRect.Height - TitleRect.Height);
                    //if (IsShowBorder)
                    //{
                    //    Height = (int)(TitleRect.Height + BorderWidth / 2 * 3);
                    //}
                    //else Height = TitleRect.Height;
                    //Invalidate();
                }
                else
                {
                    IsSpreadBody = true;
                    //Height = MyoldHeight;
                  
                }

            }
        }

        void DrawTitleText(Graphics Myg,Rectangle TitleRect)
        {
               StringFormat sf = new StringFormat();
            sf.LineAlignment = StringAlignment.Center;
            using (SolidBrush stringBrush=new SolidBrush(TitleTextColor))
            {

        
            switch (MyTitleTextAglin)
            {
                case TitleTextAglin.Center:
                    sf.Alignment = StringAlignment.Center;
                    break;
                case TitleTextAglin.Left:
                        sf.Alignment = StringAlignment.Near;
                        break;
                default:
                        sf.Alignment = StringAlignment.Center;
                        break;
            }
            Myg.DrawString(TitleText, TitleTextFont, stringBrush, TitleRect, sf);
            }

        }


        protected override void OnControlAdded(ControlEventArgs e)
        {
            int ctrlMaxtop;

            if (IsShowBorder)
            {
                if (IsSpreadBody)
                {

                    ctrlMaxtop = (int)(Height / Radius - BorderWidth);
                 
                }
                else
                {
                    ctrlMaxtop=Height;
                }
            }

            else
            {
                if (IsSpreadBody)
                {

                    ctrlMaxtop = (int)(Height / Radius);
            
                }
                else
                {
                    ctrlMaxtop = Height;

                }
            }


            if (e.Control.Top < ctrlMaxtop )
            {
                e.Control.Top = ctrlMaxtop + 2;
            }
            base.OnControlAdded(e);
        }



        ///////////////////////////////////////////////

    }
}
